package com.wantup.service.impl;

import com.wantup.multi_datasource.entity.TblUser;
import com.wantup.multi_datasource.mapper.TblUserMapper;
import com.wantup.multi_datasource2.entity.TblUser2;
import com.wantup.multi_datasource2.mapper.TblUser2Mapper;
import com.wantup.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/10/9 12:59
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private TblUserMapper tblUserMapper;

    @Autowired
    private TblUser2Mapper tblUser2Mapper;

    @Override
    @Transactional()
    public int insertUser(String username, String password) throws Exception {
        int insertUserInfo = this.tblUserMapper.insertUserInfo(username, password);
        int insertUserInfo2 = this.tblUser2Mapper.insertUserInfo(username, password);

        //int a = 1/0;

        if (insertUserInfo > 0 && insertUserInfo2 > 0) {
            return 1;
        }

        return 0;
    }


    @Override
    @Transactional()
    public int changeDataService(Integer id) throws Exception {
        TblUser tblUser = tblUserMapper.selectOneById(id);
        log.info("tblUser --> " + tblUser);
        if (tblUser != null) {
            TblUser2 tblUser2 = new TblUser2();
            BeanUtils.copyProperties(tblUser, tblUser2);
            log.info("tblUser2 --> " + tblUser2);
            try {
                int insert = tblUser2Mapper.insertUserInfo(tblUser2.getUsername(), tblUser2.getPassword());

//                int a = 1/0;

                if (insert > 0) {
                    return 1;
                } else {
                    return 0;
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("程序执行异常");
            }
        } else {
            return 0;
        }


    }
}
